package com.liu.webstore.service.impl;

import com.liu.webstore.entity.Message;
import com.liu.webstore.repository.MessageMapper;
import com.liu.webstore.service.MessageService;
import com.liu.webstore.util.SensitiveWordFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author LiuYumei
 * @Date 2022/7/28 22:55
 */
@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private SensitiveWordFilter sensitiveWordFilter;

    @Override
    public List<Message> selectConversations(int userId, int offset, int limit) {
        return messageMapper.selectConversations(userId, offset, limit);
    }

    @Override
    public int selectConversationCount(int userId) {
        return messageMapper.selectConversationCount(userId);
    }

    @Override
    public List<Message> selectLetters(String conversationId, int offset, int limit) {
        return messageMapper.selectLetters(conversationId, offset, limit);
    }

    @Override
    public int selectLetterCount(String conversationId) {
        return messageMapper.selectLetterCount(conversationId);
    }

    @Override
    public int selectLetterUnreadCount(String conversationId, int userId) {
        return messageMapper.selectLetterUnreadCount(userId, conversationId);
    }

    @Override
    public int selectSystemNoticeCount(int userId) {
        return messageMapper.selectSystemNoticeCount(userId);
    }

    @Override
    public List<Message> selectSystemNoticeList(int userId, int offert, int limit) {
        return messageMapper.selectSystemNoticeList(userId, offert, limit);
    }

    @Override
    public int saveMessage(Message message) {
        message.setContent(HtmlUtils.htmlEscape(message.getContent()));
        message.setContent(sensitiveWordFilter.filter(message.getContent()));
        return messageMapper.insertMessage(message);
    }

    @Override
    public int readMessage(List<Integer> ids) {
        return messageMapper.updateMessageStatus(ids, 1);
    }

    @Override
    public int removeLetterDetial(int letterDetailId) {
        List<Integer> ids = new ArrayList<>();
        ids.add(letterDetailId);
        return messageMapper.updateMessageStatus(ids, 2);
    }

    @Override
    public Message findLatestNotice(int userId, String topic) {
        return messageMapper.selectLatestNotice(userId, topic);
    }

    @Override
    public int findNoticeCount(int userId, String topic) {
        return messageMapper.selectNoticeCount(userId, topic);
    }

    @Override
    public int findNoticeUnreadCount(int userId, String topic) {
        return messageMapper.selectNoticeUnreadCount(userId, topic);
    }

    @Override
    public List<Message> findNoticeList(int userId, String topic, int offset, int limit) {
        return messageMapper.selectNotices(userId, topic, offset, limit);
    }
}
